package FileInformation.utils;

import FileInformation.exceptions.MissingColumnException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by IntelliJ IDEA.
 * User: Nobody
 * Date: 24.04.11
 * Time: 23:52
 * To change this template use File | Settings | File Templates.
 */
public class NumberParser {

    String regexPattern = "(-)?\\d+\\.?(\\d+)?e?(-)?(\\d+)?";
    Pattern pattern = Pattern.compile(regexPattern);

    public double[] parseCharArrayToFloatArray(char[] line, int columnSize) throws MissingColumnException, NumberFormatException {
        String strLine = String.copyValueOf(line);
        return getNumbersPresentInCharArray(strLine);
    }

    public double[] parseCharArrayToFloatArray(String line, int columnSize) throws MissingColumnException {
        return getNumbersPresentInCharArray(line);
    }

    public double[] getNumbersPresentInCharArray(String line) {

        Matcher matcher = pattern.matcher(line);

        List<String> matches = new ArrayList<String>();
        while (matcher.find()) {
            matches.add(matcher.group());
        }

        double[] doubleItems = new double[matches.size()];
        int i = -1;
        for (String item : matches) {
            doubleItems[++i] = Double.parseDouble(item);
        }
        return doubleItems;
    }

    public int countNumbersPresentInCharArray(String line) {
        return getNumbersPresentInCharArray(line).length;
    }

    public int countNumbersPresentInCharArray(char[] line) {
        String lineString = String.copyValueOf(line);
        return countNumbersPresentInCharArray(lineString);
    }
}
